/*	$Id: mib.h,v 1.8 2005/09/15 02:49:31 jfb Exp $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CANOPY_MIB_H_
#define _CANOPY_MIB_H_

#include <sys/types.h>

#include <time.h>
#include <stdint.h>


 
#define CNP_MIB_MAXNAMELEN	16
#define CNP_MIB_MAXCHLD		256
#define CNP_MIB_MAXOIDLEN	7
   
#define CNP_MIB_MAXPATHLEN    ((CNP_MIB_MAXNAMELEN + 1) * CNP_MIB_MAXOIDLEN + 4)

/* path delimiter */
#define CNP_MIB_DELIM		'.'

/* special object identifiers */
#define CNP_MIB_ROOTOID		0
#define CNP_MIB_OID_INVAL	(cnp_oid_t)-1



/* MIB flags */
#define CNP_MIB_FL_REUSE     0x01      /* recycle OIDs */
#define CNP_MIB_FL_MTLOCK    0x02      /* enable locking for multithreaded use */

 
enum cnp_mib_type {
	CNP_MIB_TYPE_BRANCH,
	CNP_MIB_TYPE_BOOL,
	CNP_MIB_TYPE_STRING,
	CNP_MIB_TYPE_INT,
	CNP_MIB_TYPE_UINT
};


enum cnp_mib_mode {
	CNP_MIB_MODE_RO,
	CNP_MIB_MODE_RW,
};



struct cnp_mib_stat {
	enum cnp_mib_type   ms_type;
	enum cnp_mib_mode   ms_mode;
	unsigned int        ms_size;
	time_t              ms_mtime;
};




/* flags */
#define CNP_MIB_FL_RDONLY   0x01    /* read-only variable */
#define CNP_MIB_FL_BOUND    0x02    /* bound variable */
 
#define IS_RO(x)     (x->mn_flags & CNP_MIB_FL_RDONLY)
#define IS_BOUND(x)  (x->mn_flags & CNP_MIB_FL_BOUND)
 

/*
 * MIB Object IDentifiers
 *
 * Each node within a MIB is identifiable by a unique object identifier, or
 * OID.  An OID is simply a sequence of unsigned integers representing
 * indexes within the parent nodes.  Each OID is encoded over 64-bits, using
 * the 7 upper bytes to store the index values, and the last byte to store
 * the length of the OID.  Any value over 7 in the length byte is considered
 * a bad OID.
 */


typedef uint64_t             cnp_oid_t;
typedef struct cnp_mib       cnp_mib_t;

typedef int (*cnp_mib_hdlr)(cnp_mib_t *mib, cnp_oid_t, void *);







cnp_mib_t* cnp_mib_alloc     (void);
void       cnp_mib_free      (cnp_mib_t *);

/* path-based API */
int        cnp_mib_mknode    (cnp_mib_t *, const char *, enum cnp_mib_type,
				enum cnp_mib_mode, int);
int        cnp_mib_stat      (cnp_mib_t *, const char *, struct cnp_mib_stat *);
int        cnp_mib_bind      (cnp_mib_t *, const char *, void *, size_t);
int        cnp_mib_unbind    (cnp_mib_t *, const char *);
int        cnp_mib_set       (cnp_mib_t *, const char *, const void *, size_t);
int        cnp_mib_get       (cnp_mib_t *, const char *, void *, size_t);
/*
int        cnp_mib_unlink    (cnp_mib_t *, const char *);
*/



/* OID-based API */
cnp_oid_t  cnp_mib_addnode    (cnp_mib_t *, cnp_oid_t, const char *,
				enum cnp_mib_type, enum cnp_mib_mode, int);
int        cnp_mib_oid_bind   (cnp_mib_t *, cnp_oid_t, void *, size_t);
int        cnp_mib_oid_unbind (cnp_mib_t *, cnp_oid_t);
int        cnp_mib_oid_set    (cnp_mib_t *, cnp_oid_t, const void *, size_t);
int        cnp_mib_oid_get    (cnp_mib_t *, cnp_oid_t, void *, size_t);
int        cnp_mib_oid_stat   (cnp_mib_t *, cnp_oid_t, struct cnp_mib_stat *);
int        cnp_mib_traverse   (cnp_mib_t *, cnp_oid_t, cnp_mib_hdlr, void *);

int        cnp_mib_pathtooid  (cnp_mib_t *, const char *, cnp_oid_t *);
char*      cnp_mib_oidtopath  (cnp_mib_t *, cnp_oid_t, char *, size_t);
char*      cnp_mib_oidtostr   (cnp_oid_t, char *, size_t);
int        cnp_mib_oidcmp     (cnp_oid_t, cnp_oid_t);

#endif /* _CANOPY_MIB_H_ */
